package com.example.mygreendao;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

//import com.hjq.permissions.OnPermission;
//import com.hjq.permissions.Permission;
//import com.hjq.permissions.XXPermissions;

import com.example.mygreendao.Dao.DaoSession;
import com.example.mygreendao.Dao.UserDao;

import java.util.List;

public class MainActivity extends AppCompatActivity {
private DaoSession daoSession;
private Button btn,btn1,btn2,Rowfind;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn = findViewById(R.id.btn);
        btn1 = findViewById(R.id.btn1);
        btn2 = findViewById(R.id.btn2);
        Rowfind = findViewById(R.id.Rowfind);
        daoSession = ((MyApp) getApplication()).getDaoSession();

//        添加数据
        User user = new User();
        user.setName("test1");
        user.setVer(2);
        daoSession.insert(user);
        Log.i("TAG", "onCreate:插入操作 "+user.toString());
        btn.setOnClickListener(v -> findall());
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                daoSession.deleteAll(User.class);
            }
        });
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                User user = new User();
                user.setId(1L);
                user.setName("小明");
                user.setVer(2);
                //插入或者替换
                daoSession.insertOrReplace(user);
            }
        });
        Rowfind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UserDao userDao = daoSession.getUserDao();
                User user1 = userDao.loadByRowId(1L);
                Log.i("TAG", "onClick: "+user1);
            }
        });

    }
    private void findall(){
        List<User> users = daoSession.loadAll(User.class);
        for (int i = 0; i <users.size() ; i++) {
            Log.i("TAG", "valuse: "+users.get(i).toString());
        }
    }
}